package com.qingxin.park.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.SamplingData;
import com.qingxin.park.service.SamplingDataService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
/**
 * 抽样检测
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/samplingData")
public class SamplingDataAction extends BaseAction{
	@Autowired
	private SamplingDataService samplingDataService;
	
	/**
	 * 跳转列表
	 * @param model
	 * @param pdttype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "/trace/samplingData/samplingData_list";
	}
	
	/**
	 * 列表数据
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search, PageParam pageParam, HttpServletRequest request){
		return samplingDataService.list(search, pageParam, getLoginUser(request));
	}
	
	/**
	 * 获取 检测单位 下拉列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getJcdwData", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<Company> getJcdwData(HttpServletRequest request){
		return samplingDataService.getJcdwData(getLoginUser(request));
	}
	
	/**
	 * 添加
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "/trace/samplingData/samplingData_add";
	}
	
	/**
	 * 保存添加
	 * @param SamplingData
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object save(@Valid SamplingData samplingData,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = samplingDataService.add(samplingData,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 删除
	 * @param samplingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] samplingid, HttpServletRequest request){
		if(samplingid==null ){
			return new SysError("没有选择任何信息.");
		}
		return samplingDataService.delete(samplingid, request);
	}
	
	/**
	 * 修改
	 * @param model
	 * @param samplingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String samplingid, HttpServletRequest request){
		SamplingData samplingData = samplingDataService.get(samplingid);
		model.put("sampling", samplingData);
		return "trace/samplingData/samplingData_edit";
	}
	
	/**
	 * 保存修改
	 * @param samplingData
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid SamplingData samplingData, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return samplingDataService.update(samplingData, getLoginUser(request), request);
	}
	
	/**
	 * 查看
	 * @param model
	 * @param samplingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/show.do", method=RequestMethod.GET)
	public String show(ModelMap model, String samplingid, HttpServletRequest request){
		SamplingData samplingData = samplingDataService.get(samplingid);
		model.put("sampling", samplingData);
		return "trace/samplingData/samplingData_show";
	}
	
}
